package com.dingding.service.user.core.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Userdetail;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.user.core.dao.IUserDetailDao;

@Component("userDetailDao")
public class UserDetailDaoImpl extends BaseDaoImpl<Userdetail> implements IUserDetailDao {

	/**
	 * 
	 * @Title: addUserDetail
	 * @Description: 保存到用户详情表
	 * @param tellphone 手机号
	 * @param userLoginId 用户ID
	 * @author CHEHAUNBO
	 * @return
	 * 
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void addUserDetail(String tellphone, String userLoginId) {

		Userdetail userdetail = new Userdetail();
		// 为用户详情表设置主键
		userdetail.setUdId(ToolUtil.getUid());
		// 用户登录ID
		userdetail.setLoginId(userLoginId);
		// 用户昵称
		userdetail.setNickName("叮叮小伙伴");
		// 用户性别
		userdetail.setSex(1);
		// 用户手机号码
		userdetail.setTellphone(tellphone);
		// 用户头像设置
		userdetail.setUserAvatar("header_2x.png");
		// 总搭乘
		userdetail.setTotalTake(new BigDecimal(0));
		// 总接程
		userdetail.setTotalWeldingProcess(new BigDecimal(0));
		// 总搭乘次数
		userdetail.setTotalTakeNumber(0);
		// 总接乘次数
		userdetail.setTotalWeldingProcessNumber(0);
		//用户信誉等级新用户默认5星
		userdetail.setUserLevel(new BigDecimal(5));
		try {
			// 向用户详情表中插入一条记录
			super.save(userdetail);
		} catch (Exception e) {
			log.error("添加用户详细信息出现异常！", e);
		}

	}

	/**
	 * @Title: getUserDetailList 
	 * @Description: 获取手机归属地为null的用户详情列表 (给所有用户详情中没有手机号归属地的加上归属地)
	 * @author YHQ 
	 * @return List<Userdetail>    返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Userdetail> getUserDetailList() {

		// 查询条件
		Criterion criterion = Restrictions.or(Restrictions.isNull("currentCity"), Restrictions.eq("currentCity", ""));

		// 用于存储手机归属地不存在的用户列表对象
		List<Userdetail> userdetails = null;
		try {
			userdetails = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("获取归属地不存在的用户列表时出现异常  类：" + this.getClass() + " getUserDetailList()", e);
		}

		return userdetails;
	}

	/**
	 * @Title: getUserdetailsByCurrentCity 
	 * @Description: 根据城市名称获取手机号（给指定城市发送短信）
	 * @param cityName 城市名称
	 * @author YHQ 
	 * @return List<Userdetail>    返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Userdetail> getUserdetailsByCurrentCity(String cityName) {

		// 查询条件
		Criterion criterion = Restrictions.ilike("currentCity", "%" + cityName + "%");

		// 用于存储手机归属地为某城市的用户列表对象
		List<Userdetail> userdetails = null;
		try {
			userdetails = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("获取指定归属地的用户列表时出现异常  类：" + this.getClass() + " getUserdetailsByCurrentCity()", e);
		}

		return userdetails;
	}

	/**
	 * @Title: updateUserDetail 
	 * @Description: 添加用户手机号所属地
	 * @param userdetail  待修改的用户详情信息
	 * @author YHQ 
	 * @return void    返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateUserDetail(Userdetail userdetail) {

		try {
			super.update(userdetail);
		} catch (Exception e) {
			log.error("添加用户手机号所属地时出现异常  类：" + this.getClass() + " updateUserDetail()", e);
		}

	}

}
